import 'package:flutter/material.dart';

import '../../common/api/work_order_api.dart';
import '../../common/type/data_list_container_type.dart';
import '../../common/type/status_tab_bar_type.dart';
import '../../common/util/common_util.dart';
import '../../page_widget/work_order_list_item.dart';
import '../../widget/data_list_container.dart';
import '../../widget/search_bar.dart';
import '../../widget/status_tab_bar.dart';

class WorkAuditPage extends StatefulWidget {
  final Map arguments;

  const WorkAuditPage({Key? key, required this.arguments}) : super(key: key);

  @override
  State<WorkAuditPage> createState() => WorkAuditPageState();
}

class WorkAuditPageState extends State<WorkAuditPage> {
  GlobalKey<DataListContainerState> dataListContainerStateKey =
      GlobalKey<DataListContainerState>();

  String keyWords = '';

  List<String> idList = [];

  String tabKey = '';
  Map tabObj = {};

  List<StatusTabBarType> tabList = [
    StatusTabBarType(key: '1', label: '备货未审核', data: {"type": 1, "status": 1}),
    StatusTabBarType(key: '2', label: '备货已审核', data: {"type": 1, "status": 2}),
    StatusTabBarType(key: '3', label: '直播间未清点', data: {"type": 2, "status": 1}),
    StatusTabBarType(key: '4', label: '直播间已清点', data: {"type": 2, "status": -1})
  ];

  int pageTotal = 0;

  @override
  void initState() {
    super.initState();
    tabKey = widget.arguments["tableKey"] ?? "1";
    for (var tab in tabList) {
      if (tab.key == tabKey) {
        tabObj = tab.data!;
        break;
      }
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  getData(DataListContainerQueryParam param) async {
    if (null != param.currentPage && param.currentPage == 1) {
      var res = tabObj["type"] == 1
          ? await WorkOrderApi.workOrderAuditNoList(
              {"keyWords": keyWords, "auditStatus": tabObj["status"]})
          : await WorkOrderApi.workOrderCheckNoList({
              "keyWords": keyWords,
              "checkUpStatus": tabObj["status"]
            });

      if (null != res.data && res.data["isSuccess"]) {
        if (null != res.data["data"] && res.data["data"].length > 0) {
          idList = ((res.data["data"]["workOrderNos"] ?? []) as List<dynamic>)
              .cast<String>();
          pageTotal = CommonUtil.calcPageTotal(idList, param.pageSize ?? 0);
          return getDataList(param.currentPage ?? 0, param.pageSize ?? 0);
        } else {
          idList = [];
          return getDataList(0, 0);
        }
      } else {
        idList = [];
        return getDataList(0, 0);
      }
    } else {
      return getDataList(param.currentPage ?? 0, param.pageSize ?? 0);
    }
  }

  getDataList(int currentPage, int pageSize) {
    return CommonUtil.getDataList(currentPage, pageSize, pageTotal, idList,
        (list) {
      return WorkOrderApi.getWorkOrderList({
        "workOrderNos": list,
      });
    });
  }

  Widget listBodyBuilder(context, item) {
    return WorkOrderListItem(
        data: item,
        toView: (data) {
          CommonUtil.cancelFocusNodes(context);
          Navigator.of(context).pushNamed("/assignWorkOrderDetail",
              arguments: {"no": data['workOrderNo']});
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("工作审核"),
      ),
      body: Column(
        children: [
          StatusTabBar(
              tableKey: tabKey,
              tabList: tabList,
              onActive: (tab, activeTabKey) {
                tabKey = activeTabKey;
                tabObj = tab.data!;
                dataListContainerStateKey.currentState!.refresh();
              }),
          SearchBar(
              hintText: '输入发起人姓名、房间号进行搜索',
              valueChanged: (value) {
                keyWords = value;
              },
              onBtnPressed: (value) {
                dataListContainerStateKey.currentState!.refresh();
              }),
          Expanded(
              child: DataListContainer(
            key: dataListContainerStateKey,
            getData: getData,
            listBodyBuilder: listBodyBuilder,
          ))
        ],
      ),
    );
  }
}
